package com.example.campus_activity_backend.controller;

import com.example.campus_activity_backend.common.BaseResponse;
import com.example.campus_activity_backend.common.ErrorCode;
import com.example.campus_activity_backend.common.ResultUtil;
import com.example.campus_activity_backend.domain.Teacher;
import com.example.campus_activity_backend.exception.BusinessException;
import com.example.campus_activity_backend.service.TeacherService;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.List;

@RequestMapping("/teacher")
@RestController
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @PostMapping("/add")
    public BaseResponse<Long> addTeacher(@RequestBody Teacher teacher){
        if(teacher == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = teacherService.addTeacher(teacher);
        return ResultUtil.success(id);
    }

    @GetMapping("/search/{id}")
    public BaseResponse<Teacher> getTeacherById(@PathVariable Integer id){
        if(id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"教师id为空");
        }
        Teacher teacher = teacherService.getById(id);
        return ResultUtil.success(teacher);
    }

    @GetMapping("/search/name/{name}")
    public BaseResponse<List<Teacher>> getTeacherByName(@PathVariable String name){
        if(name == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"教师名为空");
        }
        List<Teacher> teachers = teacherService.getTeacherByName(name);
        return ResultUtil.success(teachers);
    }
}
